1622B - Berland Music - CodeForces Solution


data structures greedy math sortings *1000

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
	n = int(input())
	p = [int(x) for x in input().split()]
	s = input()
	l = sorted([[s[i], p[i], i] for i in range(n)])
	q = [-1 for i in range(n)]
	for i in range(n):
		q[l[i][2]] = i + 1
	print(*q)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long int
#define pb push_back
const int M=1e9+7;
const int N=1e6+1;
void solve(){
    int n;
    cin>>n;
    int arr[n+1];
    for(int i=1;i<=n;i++){
        cin>>arr[i];
    }
    string s;
    cin>>s;
    map<int,int> zero,one;
    for(int i=0;i<n;i++){
        if(s[i]=='0'){
            zero[arr[i+1]] += i+1;
        }else{
            one[arr[i+1]] += i+1;
        }
    }
    int x=zero.size();
    vector<int> zeroswap,oneswap;
    for(auto it1=zero.begin();it1 != zero.end();it1++){
        if(it1->first > x){
            zeroswap.pb(it1->second);
        }
    }
    for(auto it2 = one.begin(); it2 != one.end(); it2++){
        if(it2->first <= x){
            oneswap.pb(it2->second);
        }
    }
    // cout<<oneswap.size()<<endl;
    for(int i=0;i<oneswap.size();i++){
        swap(arr[zeroswap[i]],arr[oneswap[i]]);
    }
    for(int i=1;i<=n;i++){
        cout<<arr[i]<<" ";
    }cout<<endl;
}
signed main()
{
int t;
cin>>t;
while(t--){
solve();
}
 return 0;
}


Comments

Submit
0 Comments
More Questions

1536C - Diluc and Kaeya
1428C - ABBB
1557A - Ezzat and Two Subsequences
255A - Greg's Workout
1059A - Cashier
1389C - Good String
1561A - Simply Strange Sort
1337B - Kana and Dragon Quest game
137C - History
1443C - The Delivery Dilemma
6C - Alice Bob and Chocolate
1077C - Good Array
285B - Find Marble
6A - Triangle
1729A - Two Elevators
1729B - Decode String
1729C - Jumping on Tiles
1729E - Guess the Cycle Size
553B - Kyoya and Permutation
1729D - Friends and the Restaurant
1606C - Banknotes
580C - Kefa and Park
342A - Xenia and Divisors
1033A - King Escape
39D - Cubical Planet
1453A - Cancel the Trains
645A - Amity Assessment
1144A - Diverse Strings
1553B - Reverse String
1073A - Diverse Substring